/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.dao;

import java.awt.geom.Point2D;

import org.jdom.Element;

import who.tmap.dao.TMapDAOException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.LineShape;

/**
 * @author stl
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class LineShapeDAO extends BaseMapShapeDAO {

	public MapShape load( Element elt ) throws TMapDAOException {
	    LineShape lineShape =null;
	    
        try {
            lineShape = (LineShape)msf.getGraphic( MapShapes.LINE );
            
            Element colorElt =elt.getChild( "Color" );
            lineShape.setColor( getColorFromHexa( colorElt.getAttribute( "rgb" ).getValue() ) );
            
            Element pt1Elt =elt.getChild( "Point1" );
            Point2D p1 =new Point2D.Double(
                    			pt1Elt.getAttribute( "x" ).getDoubleValue(),
                    			-pt1Elt.getAttribute( "y" ).getDoubleValue() );
            lineShape.setP1( p1 );
            
            Element pt2Elt =elt.getChild( "Point2" );
            Point2D p2 =new Point2D.Double(
                    			pt2Elt.getAttribute( "x" ).getDoubleValue(),
                    			-pt2Elt.getAttribute( "y" ).getDoubleValue() );
            lineShape.setP2( p2 );            
            
        } catch (Exception e) {
            throw new TMapDAOException( e );
        }
        return lineShape;
	}
	
	public Element save( MapShape mapShape ) throws TMapDAOException {
	    Element shapeElt =new Element( "Line" );//super.save( mapShape );
	    
        LineShape lineShape =(LineShape)mapShape;
        //shapeElt.setAttribute( "id", "Line" );
        
        Element colorElt =new Element( "Color" );
        shapeElt.addContent( colorElt );
        colorElt.setAttribute( "rgb", Integer.toHexString( lineShape.getColor().getRGB() ) );    
        
        Element pt1Elt =new Element( "Point1" );
        shapeElt.addContent( pt1Elt );
        
        pt1Elt.setAttribute( "x", Double.toString( lineShape.getP1().getX() ) );
        pt1Elt.setAttribute( "y", Double.toString( -lineShape.getP1().getY() ) );
        
        Element pt2Elt =new Element( "Point2" );
        shapeElt.addContent( pt2Elt );
        
        pt2Elt.setAttribute( "x", Double.toString( lineShape.getP2().getX() ) );
        pt2Elt.setAttribute( "y", Double.toString( -lineShape.getP2().getY() ) );

        return shapeElt;
	}
    
}
